package yaty.lee.tencent.weibo.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Logger;

import yaty.lee.tencent.weibo.exception.AuthException;

public class OAuthDataUtil {
    private static final Logger log = Logger.getLogger(OAuthDataUtil.class.getName());
    private static Properties prop = new Properties();

    static {
        String oauthFileName = "auth.properties";
        InputStream in = null;

        try {
            in = OAuthDataUtil.class.getClassLoader().getResourceAsStream(oauthFileName);
            prop.load(in);
        } catch (IOException e) {
            log.info("加载" + oauthFileName + "失败！");
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private OAuthDataUtil() {
    }

    public static String getAccessToken() {
        String accessToken = prop.getProperty("access_token");
        if (accessToken == null || "".equals(accessToken.trim())) {
            throw new AuthException();
        }
        return accessToken.trim();
    }

    public static String getExpiresIn() {
        return prop.getProperty("expires_in").trim();
    }

    public static String getOpenid() {
        return prop.getProperty("openid").trim();
    }

    public static String getOpenkey() {
        return prop.getProperty("openkey").trim();
    }

    public static String getOAuthToken() {
        return prop.getProperty("oauth_token").trim();
    }

    public static String getOAuthTokenSecret() {
        return prop.getProperty("oauth_token_secret").trim();
    }
}
